//
//  danmakuView.m
//  基本框架
//
//  Created by runone02 on 2018/7/12.
//  Copyright © 2018年 liqiyun. All rights reserved.
//

#import "danmakuView.h"
#import "DemoDanmakuItem.h"
#import "DemoDanmakuItemData.h"
#import "NSObject+FXAlertView.h"

#define CurrentDevice [UIDevice currentDevice]
#define CurrentOrientation [[UIDevice currentDevice] orientation]
#define ScreenScale [UIScreen mainScreen].scale
#define NotificationCetner [NSNotificationCenter defaultCenter]


@interface danmakuView ()<FXDanmakuDelegate>

@end
@implementation danmakuView


- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self setupUI];
    }
    return self;
}

- (void)setupUI {
    [self mainView];
    [self danmaku];
    
    self.topView = [[BtnView alloc]init];
    self.topView.titleLabel.text = @"布局";
    [self.topView.leftButton setTitle:@"头部布局" forState:UIControlStateNormal];
    [self.topView.middleBottom setTitle:@"底部布局" forState:UIControlStateNormal];
    [self.topView.rightButton setTitle:@"随机布局" forState:UIControlStateNormal];
    [self addSubview:self.topView];
    
    [self.topView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.mainView.mas_bottom).with.offset(10);
        make.left.right.mas_equalTo(self);
        make.height.mas_equalTo(80);
    }];
    
    self.zhongjianView = [[BtnView alloc]init];
    self.zhongjianView.titleLabel.text = @"添加";
    [self.zhongjianView.leftButton setTitle:@"添加一个" forState:UIControlStateNormal];
    [self.zhongjianView.middleBottom setTitle:@"添加五个" forState:UIControlStateNormal];
    [self.zhongjianView.rightButton setTitle:@"添加二十五个" forState:UIControlStateNormal];
    [self addSubview:self.zhongjianView];
    [self.zhongjianView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.topView.mas_bottom).with.offset(10);
        make.left.right.mas_equalTo(self);
        make.height.mas_equalTo(80);
    }];
    
    self.dibuView = [[BtnView alloc]init];
    self.dibuView.titleLabel.text = @"做动画";
    [self.dibuView.leftButton setTitle:@"开始" forState:UIControlStateNormal];
    [self.dibuView.middleBottom setTitle:@"推出" forState:UIControlStateNormal];
    [self.dibuView.rightButton setTitle:@"停止" forState:UIControlStateNormal];
    [self addSubview:self.dibuView];
    [self.dibuView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.zhongjianView.mas_bottom).with.offset(10);
        make.left.right.mas_equalTo(self);
        make.height.mas_equalTo(80);
    }];
    
    //布局
    [self.topView.leftButton addTarget:self action:@selector(FXDanmakuItemInsertOrderFromTop:) forControlEvents:UIControlEventTouchUpInside];
    [self.topView.middleBottom addTarget:self action:@selector(FXDanmakuItemInsertOrderFromBottom:) forControlEvents:UIControlEventTouchUpInside];
    [self.topView.rightButton addTarget:self action:@selector(FXDanmakuItemInsertOrderRandom:) forControlEvents:UIControlEventTouchUpInside];
    
    //添加的View
    [self.zhongjianView.leftButton addTarget:self action:@selector(addOneData:) forControlEvents:UIControlEventTouchUpInside];
    
    [self.zhongjianView.middleBottom addTarget:self action:@selector(addFiveData:) forControlEvents:UIControlEventTouchUpInside];
    
    [self.zhongjianView.rightButton addTarget:self action:@selector(addTwentyFiveData:) forControlEvents:UIControlEventTouchUpInside];
    
    //做动画的View
    [self.dibuView.leftButton addTarget:self action:@selector(starDanmaku:) forControlEvents:UIControlEventTouchUpInside];
    
    [self.dibuView.middleBottom addTarget:self action:@selector(pauseDanmaku:) forControlEvents:UIControlEventTouchUpInside];
    
    [self.dibuView.rightButton addTarget:self action:@selector(stopDanmaku:) forControlEvents:UIControlEventTouchUpInside];
    
}

- (UIView *)mainView{
    if (!_mainView) {
        _mainView = [[UIView alloc]init];
        [self addSubview:_mainView];
        [_mainView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.top.right.mas_equalTo(self);
            make.height.mas_equalTo(SCREEN_HEIGHT/3);
        }];
    }
    return _mainView;
}

- (FXDanmaku *)danmaku{
    if (!_danmaku) {
        _danmaku = [[FXDanmaku alloc]init];
        FXDanmakuConfiguration *config = [FXDanmakuConfiguration defaultConfiguration];
        config.rowHeight = [DemoDanmakuItem itemHeight];
        self.danmaku.configuration = config;
        self.danmaku.backgroundColor = [UIColor clearColor];
        self.danmaku.delegate = self;
        [self.danmaku registerClass:[DemoDanmakuItem class] forItemReuseIdentifier:[DemoDanmakuItem reuseIdentifier]];
        [self.mainView addSubview:_danmaku];
        [_danmaku mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.top.right.mas_equalTo(self.mainView);
            make.height.mas_equalTo(100);
        }];
    }
    return _danmaku;
}


- (void)dealloc{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (BOOL) shouldAutorotate{
    return YES;
}


- (void)setupObserver{
    [NotificationCetner addObserver:self selector:@selector(applicationWillResignActive:) name:UIApplicationWillResignActiveNotification object:nil];
    
    
    [NotificationCetner addObserver:self selector:@selector(applicationDidBecomeActive:) name:UIApplicationDidBecomeActiveNotification object:nil];
}


- (void)applicationDidBecomeActive:(NSNotification *)notification {
    [self.danmaku start];
}

- (void)applicationWillResignActive:(NSNotification*)notification {
    
    [self.danmaku pause];
}


/** 头部布局 */
- (void)FXDanmakuItemInsertOrderFromTop:(UIButton *)sender{
    FXDanmakuConfiguration *config = self.danmaku.configuration;
    config.itemInsertOrder = FXDanmakuItemInsertOrderFromTop;
    self.danmaku.configuration = config;
}

/** 底部布局 */
- (void)FXDanmakuItemInsertOrderFromBottom:(UIButton *)sender{
    FXDanmakuConfiguration *config = self.danmaku.configuration;
    
    config.itemInsertOrder = FXDanmakuItemInsertOrderFromBottom;
    self.danmaku.configuration = config;
}

/** 随机布局 */
- (void)FXDanmakuItemInsertOrderRandom:(UIButton *)sender{
    FXDanmakuConfiguration *config = self.danmaku.configuration;
    config.itemInsertOrder = FXDanmakuItemInsertOrderRandom;
    self.danmaku.configuration = config;
}


/**
 添加一个
 */
- (void) addOneData:(UIButton *)sender{
    NSLog(@"添加一个");
    [self addDatasWithCount:1];
}

/** 添加五个 */
- (void)addFiveData:(UIButton *)sender{
    NSLog(@"添加五个");
    [self addDatasWithCount:5];
}

/** 添加二十五个 */
- (void)addTwentyFiveData:(UIButton *)sender{
    NSLog(@"添加二十五个");
    [self addDatasWithCount:25];
}


/** 开始 */
- (void)starDanmaku:(UIButton *)sender{
    NSLog(@"开始");
    [self.danmaku start];
}

/** 推出 */
- (void)pauseDanmaku:(UIButton *)sender{
    NSLog(@"推出");
    [self.danmaku pause];
}


/** 停止 */
- (void)stopDanmaku:(UIButton *)sender{
    NSLog(@"停止");
    [self.danmaku stop];
}


//点击滚动条
- (void)danmaku:(FXDanmaku *)danmaku didClickItem:(FXDanmakuItem *)item withData:(DemoDanmakuItemData *)data{
    [self fx_presentConfirmViewWithTitle:nil message:[NSString stringWithFormat:@"你点击了%@",data.desc] confirmButtonTitle:nil cancelButtonTitle:@"OK" confirmHandler:nil cancelHandler:nil];
}

- (BOOL)shouldAddDanmakuItemDataWhenQueueIsFull:(FXDanmakuItemData *)data {
    
    return FXDataPriorityHigh == data.priority;
}

/** 添加的个数 */
- (void)addDatasWithCount:(NSInteger)count{
    static NSUInteger index = 0;
    
    for (NSUInteger i = 0; i < count; i ++) {
        DemoDanmakuItemData *data = [DemoDanmakuItemData data];
//        data.avatarName = [NSString stringWithFormat:@"什么鬼 %u",arc4random()%6];
        data.desc = [NSString stringWithFormat:@"什么鬼JJ %@",@(index++)];
        [self.danmaku addData:data];
    }
    
    if (!self.danmaku.isRunning) {
        [self.danmaku start];
    }
}


- (void) viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator{
    [self.danmaku pause];
    
    [coordinator animateAlongsideTransition:nil completion:^(id<UIViewControllerTransitionCoordinatorContext>  _Nonnull context) {
        [self.danmaku start];
    }];
}


- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration{
    [self.danmaku pause];
}

- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation{
    [self.danmaku start];
}


@end

@implementation BtnView

- (UIView *)mainView{
    if (!_mainView) {
        _mainView = [[UIView alloc]init];
        [self addSubview:_mainView];
        [_mainView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.left.right.bottom.mas_equalTo(self);
        }];
    }
    return _mainView;
}

- (UILabel *)titleLabel{
    if (!_titleLabel) {
        _titleLabel = [[UILabel alloc]init];
        _titleLabel.font = TITLE_FONT(30);
        _titleLabel.textColor = [UIColor blackColor];
        _titleLabel.text = @"标题";
        _titleLabel.textAlignment = NSTextAlignmentCenter;
        [self.mainView addSubview:_titleLabel];
        [_titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(self.mainView).with.offset(10);
            make.left.mas_equalTo(self.mainView).with.offset(30);
            make.right.mas_equalTo(self.mainView).with.offset(-30);
            make.centerX.mas_equalTo(self.mainView);
        }];
    }
    return _titleLabel;
}

- (UIButton *)middleBottom{
    if (!_middleBottom) {
        _middleBottom = [UIButton buttonWithType:UIButtonTypeCustom];
        _middleBottom.titleLabel.font = TITLE_FONT(30);
        _middleBottom.layer.masksToBounds = YES;
        _middleBottom.layer.cornerRadius = 3;
        _middleBottom.layer.borderColor = [UIColor blackColor].CGColor;
        _middleBottom.layer.borderWidth = 1;
        [_middleBottom setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [self.mainView addSubview:_middleBottom];
        [_middleBottom mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.mas_equalTo(self.mainView);
            make.top.mas_equalTo(self.titleLabel.mas_bottom).with.offset(10);
            make.width.mas_equalTo(SCREEN_WIDTH/3 - 10);
        }];
    }
    return _middleBottom;
}

- (UIButton *)leftButton{
    if (!_leftButton) {
        _leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _leftButton.titleLabel.font = TITLE_FONT(30);
        _leftButton.layer.cornerRadius = 3;
        _leftButton.layer.borderWidth = 1;
        _leftButton.layer.borderColor = [UIColor blackColor].CGColor;
        _leftButton.layer.masksToBounds = YES;
        [_leftButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [self.mainView addSubview:_leftButton];
        [_leftButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.mas_equalTo(self.middleBottom);
            make.left.mas_equalTo(self.mainView).with.offset(5);
            make.right.mas_equalTo(self.middleBottom.mas_left).with.offset(-5);
        }];
    }
    return _leftButton;
}


- (UIButton *)rightButton{
    if (!_rightButton) {
        _rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _rightButton.titleLabel.font = TITLE_FONT(30);
        _rightButton.layer.cornerRadius = 3;
        _rightButton.layer.borderWidth = 1;
        _rightButton.layer.borderColor = [UIColor blackColor].CGColor;
        _rightButton.layer.masksToBounds = YES;
        [_rightButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [self.mainView addSubview:_rightButton];
        [_rightButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(self.middleBottom.mas_right).with.offset(5);
            make.centerY.mas_equalTo(self.middleBottom);
            make.right.mas_equalTo(self.mainView).with.offset(-5);            
        }];
    }
    return _rightButton;
}

@end
